﻿using System.Linq;
using AutoMapper;
using Colorful.Furniture.EnterpriseManagement;
using Colorful.Furniture.Extensions;
using Colorful.Furniture.SysManagement;

namespace Colorful.Furniture.PermissionManagement;

/// <summary>
///  权限管理映射配置
/// </summary>
public class PermissionAutoMapperProfile : Profile
{
    /// <summary>
    ///  自动映射
    /// </summary>
    public PermissionAutoMapperProfile()
    {
        #region 管理员及权限配置

        //****************************输入************************ 

        //角色管理
        CreateMap<RoleSaveInputDto, SysRole>();
        CreateMap<SetRoleDataPermissionInputDto, SysDataPermission>();

        //修改管理员/密码
        CreateMap<ManagerPasswordSaveInputDto, SysManagerIdentity>();
        CreateMap<ManagerPasswordSaveInputDto, SysManager>();
        CreateMap<ManagerSaveInputDto, SysManager>()
            .ForMember(x => x.Birthday, y => y.MapFrom(z => z.Birthday.ToNullableDateTime()))
            .ForMember(x => x.DepartmentId, y => y.MapFrom(z => z.DepartmentIdList.Last()));

        //管理员授权表
        CreateMap<SysManagerIdentitySaveInputDto, SysManagerIdentity>();

        //权限
        CreateMap<SetPermissionInputDto, SysPermission>();

        //接口
        CreateMap<SysApiInputDto, SysApi>();

        //资源
        CreateMap<SyncResourceInputDto, SysResource>()
            .ForMember(x => x.Url, y => y.MapFrom(a => a.Path))
            .ForMember(x => x.Title, y => y.MapFrom(a => a.Meta.Title))
            .ForMember(x => x.ImgUrl, y => y.MapFrom(a => a.Meta.Icon))
            .ForMember(x => x.IsHide, y => y.MapFrom(a => a.Hidden));

        //****************************输出************************

        //角色管理
        CreateMap<SysRole, RoleOutputDto>();

        //管理员编辑
        CreateMap<SysManager, ManagerOutputDto>();
        CreateMap<SysManager, ManagerLoginOutputDto>();
        CreateMap<SysManager, ManagerPageOutputDto>();
        CreateMap<SysManager, ManagerNormalOutputDto>();
        CreateMap<SysManager, ManagerPasswordSaveInputDto>();

        // 登录
        CreateMap<ManagerLoginOutputDto, ManagerInfoOutputDto>();
        CreateMap<ManagerInfoOutputDto, ManagerLoginOutputDto>();

        //资源和机构
        CreateMap<SysResource, PermissionMenuOutputDto>();
        CreateMap<Department, OrganizationDto>();

        //管理员授权表
        CreateMap<SysManagerIdentity, SysManagerIdentityOutputDto>();

        // 管理员授权表映射
        CreateMap<SysManager, ManagerNormalOutputDto>()
            .ForMember(x => x.ManagerIdentityList, y => y.MapFrom(a => a.ManagerIdentities));

        //接口
        CreateMap<SysApi, SysApiOutputDto>();

        #endregion
    }
}
